﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Web.Security;
using Microsoft.ApplicationBlocks.Data;
using visinia.SmartEngine.Providers;

namespace visinia.SmartEngine.SqlDataProviders.Security
{
    public class visiniaRoleProvider : RoleProvider
    {
        #region INIT
        private static string _sqlConnectionString = null;
        public string ConnectionString
        {
            get { return _sqlConnectionString; }
        }
        public override void Initialize(string name, NameValueCollection config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }
            base.Initialize(name, config);            
            _sqlConnectionString = ProviderController.GetDataProviderConnectionString();
        }
        #endregion

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            foreach (string username in usernames)
            {
                foreach (string rolename in roleNames)
                {
                    SqlHelper.ExecuteNonQuery(ConnectionString, "SecurityAddUserToRole", username, rolename);
                }
            }
        }

        public override void CreateRole(string RoleName)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "SecurityCreateRole", RoleName);
        }

        public override bool DeleteRole(string RoleName, bool throwOnPopulatedRole)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "SecurityDeleteRole", RoleName);
            return true;
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            throw new NotImplementedException();
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            foreach (string username in usernames)
            {
                foreach (string rolename in roleNames)
                {
                    SqlHelper.ExecuteNonQuery(ConnectionString, "SecurityRemoveUserFromRole", username, rolename);
                }
            }
        }

        const string _ApplicationName = "visinia";

        public override string ApplicationName
        {
            get { return _ApplicationName; }
            set { throw new NotImplementedException(); }
        }

        public override string[] GetAllRoles()
        {
            return FillRoles(SqlHelper.ExecuteReader(ConnectionString, "SecurityGetAllRoles")).ToArray();

        }

        public override string[] GetRolesForUser(string username)
        {
            return FillRoles(SqlHelper.ExecuteReader(ConnectionString, "SecurityGetRolesForUser", username)).ToArray();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }

        #region UTILITY
        private static List<string> FillRoles(IDataReader dr)
        {
            List<string> list = new List<string>();
            while (dr.Read())
            {
                if (dr["RoleName"] != Convert.DBNull)
                    list.Add((string)dr["RoleName"]);
            }
            return list;
        }
        #endregion
    }
}
